package com.miao.shop.controller;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.miao.shop.config.AliPayConfig;
import com.miao.shop.service.AlipayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Miaowenqing
 * @date 2021-02-01 20:36
 */
@RestController
@RequestMapping("common/pay")
public class AliPayController {
    private AlipayService alipayService;

    @Autowired
    public void setAlipayService(AlipayService alipayService) {
        this.alipayService = alipayService;
    }

    @PostMapping(value = "/getNotifyUrlInfo")
    public void alipayNotifyUrlInfo(HttpServletRequest request, HttpServletResponse response){

        alipayService.notify(request,response);
    }
    /**
     * web 订单支付
     */
    @GetMapping("/getPagePay")
    @CrossOrigin
    public String getPagePay(String orderId,Double price) throws Exception{

        String response= this.alipayService.webPagePay(orderId,price,"仅用于学习");
        return response;
    }

    /**
     * app 订单支付
     */
    @GetMapping("getAppPagePay")
    public Object getAppPagePay() throws Exception{
        /** 模仿数据库，从后台调数据*/
        String outTradeNo = "131233";
        Integer totalAmount = 1000;
        String subject = "天猫超市012";

        String pay = alipayService.appPagePay(outTradeNo, totalAmount, subject);

        Object json = JSONObject.toJSON(pay);

        System.out.println(json);

        return json;
    }

    /**
     * 交易查询
     */
    @PostMapping("aipayQuery")
    public Object alipayQuery() throws Exception{
        /**调取支付订单号*/
        String outTradeNo = "13123";

        String query = alipayService.query(outTradeNo);

        Object json = JSONObject.toJSON(query);

        /*JSONObject jObject = new JSONObject();
        jObject.get(query);*/
        return json;
    }

    /**
     * 退款
     * @throws AlipayApiException
     */
    @GetMapping("alipayRefund")
    public Object alipayRefund(
            @RequestParam("outTradeNo")String outTradeNo,
            @RequestParam(value = "outRequestNo", required = false)String outRequestNo,
            @RequestParam(value = "refundAmount", required = false)Integer refundAmount
    ) throws AlipayApiException{

        /** 调取数据*/
        //String outTradeNo = "15382028806591197";
        String refundReason = "用户不想购买";
        //refundAmount = 1;
        //outRequestNo = "22";

        String refund = alipayService.refund(outTradeNo, refundReason, refundAmount, outRequestNo);

        System.out.println(refund);

        return refund;
    }

    /**
     * 退款查询
     * @throws AlipayApiException
     */
    @PostMapping("refundQuery")
    public Object refundQuery() throws AlipayApiException{

        /** 调取数据*/
        String outTradeNo = "13123";
        String outRequestNo = "2";

        String refund = alipayService.refundQuery(outTradeNo, outRequestNo);

        return refund;

    }

    /**
     * 交易关闭
     * @throws AlipayApiException
     */
    @PostMapping("alipayclose")
    public Object alipaycolse() throws AlipayApiException {

        /** 调取数据*/
        String outTradeNo = "13123";

        String close = alipayService.close(outTradeNo);

        return close;
    }


}


